<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Model;

class Collection extends Model{
    protected $autoWriteTimestamp = true;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*'){
        $list = $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,username,nickname,sex,head,rong_token');
            },'article'])
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $collection = new Collection();
        $message = new Message();
        foreach ($list as $k => $v){
            $list[$k]['message'] = $message->where('aid','eq',$v['id'])->count();
            $list[$k]['collection'] = $collection->where('aid','eq',$v['id'])->count();
        }
        return $list;
    }

    /**
     * @param int $id
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id=0){
        $info = $this->find($id);
        $info->hits = $info['hits'] + 1;
        $info->save();
        $_info = $this
            ->with(['user'=>function($query){
                $query->field('id,username,nickname,sex,head,rong_token');
            },'article'=>function($query){
                $query->field('*');
            }])
            ->where('id','eq',$id)
            ->find();
        $collection = new Collection();
        $message = new Message();
        $_info['message'] = $message->where('aid','eq',$_info['id'])->count();
        $_info['collection'] = $collection->where('aid','eq',$_info['id'])->count();
        return $_info;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    public function article(){
        return $this->hasOne('article','id','aid');
    }

    /**
     * 收藏
     * @param int $mid
     * @param int $aid
     * @param int $type
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collection($mid=0,$aid=0,$type=1,&$out=''){
        if(!$mid || !$aid){
            $out = '缺少参数';
            return false;
        }
        $info = $this
            ->where('mid','eq',$mid)
            ->where('aid','eq',$aid)
            ->find();
        if($type==1){
            if($info){
                $out = '你已经收藏了';
                return false;
            }
            if(!$this->save([
                'mid'=>$mid,
                'aid'=>$aid
            ])){
                $out = '收藏失败';
                return false;
            }
            return true;
        }else{
            if(!$info){
                $out = '你还没有收藏';
                return false;
            }
            if(!$info->delete()){
                $out = '取消收藏失败';
                return false;
            }
            return true;
        }
    }
}